<!DOCTYPE html>
<html lang="en">

<!-- Head tag -->
<head><meta name="generator" content="Hexo 3.8.0">
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="江湖郎中点滴">
    <meta name="keyword" content>
    <meta name="theme-color" content="#600090">
    <meta name="msapplication-navbutton-color" content="#600090">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="#600090">
    <link rel="shortcut icon" href="https://cdn4.iconfinder.com/data/icons/ionicons/512/icon-person-128.png">
    <link rel="alternate" type="application/atom+xml" title="江湖郎中" href="/atom.xml">
    <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/animate.css/3.5.2/animate.min.css">
    <link rel="stylesheet" href="//cdn.bootcss.com/font-awesome/4.6.3/css/font-awesome.css">
    <title>
        
        Cypher使用简介｜江湖郎中的博客，从事互联网行业，立志点点滴滴解决遇到的疑难杂症
        
    </title>

    <link rel="canonical" href="http://bitlove.cn/2019/04/13/cypher试用简介/">

    <!-- Bootstrap Core CSS -->
    <link rel="stylesheet" href="/css/bootstrap.min.css">

    <!-- Custom CSS -->
    <link rel="stylesheet" href="/css/blog-style.css">

    <!-- Pygments Github CSS -->
    <link rel="stylesheet" href="/css/syntax.css">
</head>

<style>

    header.intro-header {
        background-image: url('')
    }
</style>
<!-- hack iOS CSS :active style -->
<body ontouchstart="" class="animated fadeIn">
<!-- Navigation -->
<nav class="navbar navbar-default navbar-custom navbar-fixed-top " id="nav-top" data-ispost="true" data-istags="false
" data-ishome="false">
    <div class="container-fluid">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header page-scroll">
            <button type="button" class="navbar-toggle">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand animated pulse" href="/">
                <span class="brand-logo">
                    江湖郎中
                </span>
                's Blog
            </a>
        </div>

        <!-- Collect the nav links, forms, and other content for toggling -->
        <!-- Known Issue, found by Hux:
            <nav>'s height woule be hold on by its content.
            so, when navbar scale out, the <nav> will cover tags.
            also mask any touch event of tags, unfortunately.
        -->
        <!-- /.navbar-collapse -->
        <div id="huxblog_navbar">
            <div class="navbar-collapse">
                <ul class="nav navbar-nav navbar-right">
                    <li>
                        <a href="/">Home</a>
                    </li>
					
                    
                        
							
                        <li>
                            <a href="/tags/">tags</a>
                        </li>
							
						
                    
					
					
                </ul>
            </div>
        </div>
    </div>
    <!-- /.container -->
</nav>
<script>
    // Drop Bootstarp low-performance Navbar
    // Use customize navbar with high-quality material design animation
    // in high-perf jank-free CSS3 implementation
//    var $body   = document.body;
    var $toggle = document.querySelector('.navbar-toggle');
    var $navbar = document.querySelector('#huxblog_navbar');
    var $collapse = document.querySelector('.navbar-collapse');

    $toggle.addEventListener('click', handleMagic)
    function handleMagic(e){
        if ($navbar.className.indexOf('in') > 0) {
        // CLOSE
            $navbar.className = " ";
            // wait until animation end.
            setTimeout(function(){
                // prevent frequently toggle
                if($navbar.className.indexOf('in') < 0) {
                    $collapse.style.height = "0px"
                }
            },400)
        }else{
        // OPEN
            $collapse.style.height = "auto"
            $navbar.className += " in";
        }
    }
</script>

<!-- Main Content -->

<!--only post-->


<img class="wechat-title-img" src>


<style>
    
    header.intro-header {
        background-image: url('')
    }

    
</style>

<header class="intro-header">
    <div class="container">
        <div class="row">
            <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1 text-center">
                <div class="post-heading">
                    <h1>Cypher使用简介</h1>
                    
                    <span class="meta">
                         作者 江湖郎中
                        <span>
                          日期 2019-04-13
                         </span>
                    </span>
                    <div class="tags text-center">
                        
                        <a class="tag" href="/tags/#neo4j" title="neo4j">neo4j</a>
                        
                        <a class="tag" href="/tags/#知识图谱" title="知识图谱">知识图谱</a>
                        
                        <a class="tag" href="/tags/#Cypher" title="Cypher">Cypher</a>
                        
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="post-title-haojen">
        <span>
            Cypher使用简介
        </span>
    </div>
</header>

<!-- Post Content -->
<article>
    <div class="container">
        <div class="row">
            <!-- Post Container -->
            <div class="col-lg-8 col-lg-offset-1 col-sm-9 post-container">
                <h2 id="概述"><a href="#概述" class="headerlink" title="概述"></a>概述</h2><p>Cypher, The Graph Query Language<br>The Standard Query Language for Graph Database Technology</p>
<p>这是官方给出的定义，简单来说就是 图数据库的查询语言，一种图数据库标准化查询语言的技术<br>理解起来比较容易，他从<code>SPARQL</code>中在概念上获得了很多的启发。<br><code>Cypher</code>描述节点、关系、属性上面像<code>ASCII</code>码一样通俗易懂，它使查询变得简单，由于他的逻辑非常清晰，使得在维护应用的时候也变得很简单</p>
<h2 id="使用说明"><a href="#使用说明" class="headerlink" title="使用说明"></a>使用说明</h2><h3 id="Create"><a href="#Create" class="headerlink" title="Create"></a>Create</h3><p>增加、创建是我们用到的第一个命令，在neo4j中，节点不存在名字的概念，更多的是以label的形式来表达，一个节点可以有多个label，用来从不同维度来描述这个节点</p>
<ul>
<li><code>CREATE (n:$label {name: $value})</code> 创建一个节点，指定一个标签<code>$label</code> 并且赋予指定属性属性</li>
<li><code>CREATE (n)-[r:KNOWS]-&gt;(m)</code> 创建一个关系，并指定了类型和方向，以及将其绑定到一个变量<code>r</code>上</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">比如，我创建一个类型为`互联网`的节点,有个属性ename，值为Internet,就可以按照下面的语句去写</span><br><span class="line">CREATE (n:互联网 &#123;ename:&apos;Internet&apos;&#125;)</span><br><span class="line"></span><br><span class="line">这里面的`n`只是变量，方便在复杂语句中引用使用，如果用不到可以不写，那么就可以简写为</span><br><span class="line"></span><br><span class="line">CREATE(:互联网 &#123;ename:&apos;Internet&apos;&#125; )</span><br><span class="line"></span><br><span class="line">又比如，我想创建一个有`即时通讯`的标签，同时他有一个属性 ename，值为 IM，我就可以这样写</span><br><span class="line"></span><br><span class="line">CREATE (n:即时通讯 &#123;ename:&apos;IM&apos;&#125;)</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">如果想创建两个类型的节点，一个是互联网，一个是即时通讯，同时我想建立一个关系`即时通讯属于互联网`，那么应该怎么写呢</span><br><span class="line"></span><br><span class="line">Create (:即时通讯)-[r:belong]-&gt;(:互联网)</span><br></pre></td></tr></table></figure>
<p>如图所示<br><img src="https://note.youdao.com/yws/public/resource/c537253ff87f0a68329d8b25970e10e9/xmlnote/50D113B920B04B49999B586B76188079/15859" alt="image"></p>
<h3 id="Query"><a href="#Query" class="headerlink" title="Query"></a>Query</h3><p>有了数据，就要去使用，使用的前提要能拿到这些数据，就是需要进行query</p>
<ul>
<li><code>MATCH (n) RETURN n</code> 查询节点，如果没有输入查询条件则是查询所有节点</li>
<li><code>match(n:互联网) return n</code> 查询带有互联网标签的节点</li>
<li><code>match(n {ename:&#39;IM&#39;}) return n;</code> 查询带有ename属性，并且值为IM的节点</li>
<li><code>match(n) where n.ename=&#39;IM&#39; return n</code> 查询带有ename属性，并且值为IM的节点</li>
<li><code>match (m:即时通讯)-[r:belong]-&gt;(:互联网) return m;</code> 查询满足某种关系的节点</li>
</ul>
<h3 id="Update"><a href="#Update" class="headerlink" title="Update"></a>Update</h3><p>更新是频繁使用的一个操作，我们会去更新节点属性，我们会去更新标签、关系等等，通常是先查询出来，然后再赋值，如果原来没有就新增</p>
<ul>
<li><code>match(n:即时通讯) set n.sub=&#39;qq&#39; return n;</code>  新增一个节点的属性</li>
<li><code>match(n:即时通讯) set n:聊天 return n;</code> 新增一个标签</li>
</ul>
<h3 id="Delete"><a href="#Delete" class="headerlink" title="Delete"></a>Delete</h3><p>删除也是必不可少的操作</p>
<ul>
<li><code>MATCH (n:Person { name: &#39;UNKNOWN&#39; }) DELETE n</code> 删除匹配到的节点</li>
<li><code>MATCH (n { name: &#39;Andy&#39; }) DETACH DELETE n</code> 删除节点及关联的关系，重点是<code>DETACH DELETE</code></li>
<li><code>MATCH (n { name: &#39;Andy&#39; })-[r:KNOWS]-&gt;() DELETE r</code> 只删除关系</li>
</ul>
<h2 id="后记"><a href="#后记" class="headerlink" title="后记"></a>后记</h2><p>上面简单记录了Cypher对于增删改查的使用，当然Cyper的能力远不止上面这些，还有很多复杂的功能，不过万变不离其宗，理解了这些基础操作，那么在这些基础进行复杂的查询、操作就会有迹可循了。以上内容参考<a href="https://neo4j.com/docs/cypher-manual/3.5" target="_blank" rel="noopener">https://neo4j.com/docs/cypher-manual/3.5</a> , 里面有更详细的介绍。</p>

                <hr>
                

                <ul class="pager">
                    
                    
                    <li class="next">
                        <a href="/2019/04/12/图数据库分析/" data-toggle="tooltip" data-placement="top" title="图数据库分析">Next Post &rarr;</a>
                    </li>
                    
                </ul>

                

                


                <!--加入新的评论系统-->
                

                

            </div>

            <div class="hidden-xs col-sm-3 toc-col">
                <div class="toc-wrap">
                    <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#概述"><span class="toc-text">概述</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#使用说明"><span class="toc-text">使用说明</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#Create"><span class="toc-text">Create</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Query"><span class="toc-text">Query</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Update"><span class="toc-text">Update</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Delete"><span class="toc-text">Delete</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#后记"><span class="toc-text">后记</span></a></li></ol>
                </div>
            </div>
        </div>

        <div class="row">
            <!-- Sidebar Container -->

            <div class="
                col-lg-8 col-lg-offset-2
                col-md-10 col-md-offset-1
                sidebar-container">

                <!-- Featured Tags -->
                

                <!-- Friends Blog -->
                
            </div>
        </div>

    </div>
</article>







<!-- Footer -->
<!-- Footer -->
<footer>
    <div class="container">
        <div class="row">
            <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1 text-center">
                <br>
                <ul class="list-inline text-center">
                
                
                

                

                

                

                

                </ul>
                <p class="copyright text-muted">
                    Copyright &copy; 江湖郎中 2019
                    <br>
                    <span id="busuanzi_container_site_pv" style="font-size: 12px;">PV: <span id="busuanzi_value_site_pv"></span> Times</span>
                    <br>
                    Theme by <a href="https://haojen.github.io/">Haojen Ma</a>
                </p>

            </div>
        </div>
    </div>
</footer>

<!-- jQuery -->
<script src="/js/jquery.min.js"></script>

<!-- Bootstrap Core JavaScript -->
<script src="/js/bootstrap.min.js"></script>

<!-- Custom Theme JavaScript -->
<script src="/js/blog.js"></script>

<!-- async load function -->
<script>
    function async(u, c) {
      var d = document, t = 'script',
          o = d.createElement(t),
          s = d.getElementsByTagName(t)[0];
      o.src = u;
      if (c) { o.addEventListener('load', function (e) { c(null, e); }, false); }
      s.parentNode.insertBefore(o, s);
    }
</script>

<!-- jquery.tagcloud.js -->
<script>
    // only load tagcloud.js in tag.html
    if($('#tag_cloud').length !== 0){
        async("http://bitlove.cn/js/jquery.tagcloud.js",function(){
            $.fn.tagcloud.defaults = {
                //size: {start: 1, end: 1, unit: 'em'},
                color: {start: '#bbbbee', end: '#0085a1'},
            };
            $('#tag_cloud a').tagcloud();
        })
    }
</script>

<!--fastClick.js -->
<script>
    async("//cdn.bootcss.com/fastclick/1.0.6/fastclick.min.js", function(){
        var $nav = document.querySelector("nav");
        if($nav) FastClick.attach($nav);
    })
</script>



<!-- Google Analytics -->



<!-- Baidu Tongji -->


<!-- swiftype -->
<script type="text/javascript">
  (function(w,d,t,u,n,s,e){w['SwiftypeObject']=n;w[n]=w[n]||function(){
  (w[n].q=w[n].q||[]).push(arguments);};s=d.createElement(t);
  e=d.getElementsByTagName(t)[0];s.async=1;s.src=u;e.parentNode.insertBefore(s,e);
  })(window,document,'script','//s.swiftypecdn.com/install/v2/st.js','_st');

  _st('install','','2.0.0');
</script>

<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>

<!--wechat title img-->
<img class="wechat-title-img" src="/image/header.jpg">
</body>

</html>
